﻿//-----------------------------------------------------------------------
// <copyright file="EntityExtensions.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace TileTool.Scene.Entity
{
    using System;
    using TileTool.Common.Math;

    /// <summary>
    /// Class which contains entity extension methods.
    /// </summary>
    public static class EntityExtensions
    {
        /// <summary>
        /// Gets the center point of an entity.
        /// </summary>
        /// <param name="entity">Entity to get the center of.</param>
        /// <returns>Returns the center point of the entity.</returns>
        public static Vector2D GetCenter(this IEntity entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity", "Entity must be non-null.");
            }

            Size2D size = entity.Size;
            Matrix2D mat = entity.Matrix;
            return entity.Pos + (mat.X * size.Width * 0.5f) + (mat.Y * size.Height * 0.5f);
        }

        /// <summary>
        /// Will calculate the points in world space that make up the rectangle bounds of this object.
        /// 
        /// The returned vector should always be of size 4 and the points will be ordered as follows:
        ///     Point 0 - Lower left
        ///     Point 1 - Lower right
        ///     Point 2 - Upper right
        ///     Point 3 - Upper left
        ///     
        /// Note that due to the entity's matrix the lower left point may not necessarily be in the
        /// lower left in world space, but the lower left point should always be at index 0.
        /// </summary>
        /// <param name="entity">Entity to get the rect points for.</param>
        /// <returns>Returns an array of world space points which make up the bounds of this object.</returns>
        public static Vector2D[] GetRectanglePoints(this IEntity entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity", "Entity must be non-null.");
            }

            Vector2D[] res = new Vector2D[4];
            Matrix2D mat = entity.Matrix;
            Size2D size = entity.Size;

            res[0] = mat.Pos;
            res[1] = res[0] + (mat.X * size.Width);
            res[2] = res[1] + (mat.Y * size.Height);
            res[3] = res[0] + (mat.Y * size.Height);

            return res;
        }
    }
}
